/**
 * Notes: 资讯后台管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux@qq.com
 * Date: 2021-07-11 07:48:00 
 */

const BaseAdminService = require('./base_admin_service.js');
const NewsModel = require('../../model/news_model.js');
const util = require('../../../framework/utils/util.js');
const timeUtil = require('../../../framework/utils/time_util.js');

class AdminNewsService extends BaseAdminService {

    /** 资讯列表 */
    async getNewsList({
        search, // 搜索条件
        sortType, // 搜索菜单
        sortVal, // 搜索菜单
        orderBy, // 排序
        whereEx, //附加查询条件
        page,
        size,
        isTotal = true,
        oldTotal
    }) {

        orderBy = orderBy || {
            'NEWS_ORDER': 'asc',
            'NEWS_ADD_TIME': 'desc'
        };
        let fields = '*';

        let where = {};
        where.NEWS_STATUS = 1;

        if (util.isDefined(search) && search) {
            where.NEWS_TITLE = {
                $regex: '.*' + search,
                $options: 'i'
            };
        } else if (sortType && util.isDefined(sortVal)) {
            switch (sortType) {
                case 'cateId':
                    where.NEWS_CATE_ID = sortVal;
                    break;
                case 'status':
                    where.NEWS_STATUS = Number(sortVal);
                    break;
                case 'sort':
                    if (sortVal == 'view') {
                        orderBy = {
                            'NEWS_VIEW_CNT': 'desc',
                            'NEWS_ADD_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'new') {
                        orderBy = {
                            'NEWS_ADD_TIME': 'desc'
                        };
                    }
                    break;
            }
        }

        return await NewsModel.getList(where, fields, orderBy, page, size, isTotal, oldTotal);
    }

    /** 更新资讯数据 */
    async editNews({
        id,
        title,
        cateId,
        cateName,
        order,
        type = 0,
        desc = '',
        url = '',
    }) {
        let data = {
            NEWS_TITLE: title,
            NEWS_CATE_ID: cateId,
            NEWS_CATE_NAME: cateName,
            NEWS_ORDER: order,
            NEWS_TYPE: type,
            NEWS_DESC: desc,
            NEWS_URL: url
        }

        await NewsModel.edit({
            _id: id
        }, data);
    }

    /** 删除资讯 */
    async delNews(id) {
        await NewsModel.del({
            _id: id
        });
    }

    /** 添加资讯 */
    async insertNews({
        title,
        cateId,
        cateName,
        order,
        type = 0,
        desc = '',
        url = '',
    }) {
        let data = {
            NEWS_TITLE: title,
            NEWS_CATE_ID: cateId,
            NEWS_CATE_NAME: cateName,
            NEWS_ORDER: order,
            NEWS_TYPE: type,
            NEWS_DESC: desc,
            NEWS_URL: url,
            NEWS_STATUS: 1,
            NEWS_ADD_TIME: timeUtil.time(),
        }

        await NewsModel.insert(data);
    }
}

module.exports = AdminNewsService;